﻿using Castle.MicroKernel.Registration;
using Castle.MicroKernel.SubSystems.Configuration;
using Castle.Windsor;
using SuppKids.Core.Reporting;
using SuppKids.Core.Services;
using SuppKids.Core.Stuff;

namespace SuppKids.IoC.Installers
{
	public class ServicesInstaller : IWindsorInstaller
	{
		public void Install(IWindsorContainer container, IConfigurationStore store)
		{
			container.Register(FindServices().Configure(x => x.LifeStyle.Transient.Configuration()));
			container.Register(Component.For<ReportGeneratorFactory>().ImplementedBy<ReportGeneratorFactory>().LifeStyle.Singleton);

			container.Register(Component.For<StuffInfoStorage>().ImplementedBy<StuffInfoStorage>().LifeStyle.Singleton);
		}

		private static BasedOnDescriptor FindServices()
		{
			var res = Types.FromAssemblyContaining(typeof(IService))
				.BasedOn(typeof(IService))
				.If(t => t.Name.EndsWith("Service"))
				.WithService
				.Self();

			return res;
		}
	}
}
